package com.thinkcube.edutainment.fragment;

import java.util.ArrayList;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.thinkcube.edutainment.EdutainmentConstant;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.ReachabilityTest;
import com.thinkcube.edutainment.adapters.GlossaryAdapter;
import com.thinkcube.edutainment.adapters.GlossaryIndexAdapter;
import com.thinkcube.edutainment.client.GlossaryClient;
import com.thinkcube.edutainment.client.UserClient;
import com.thinkcube.edutainment.client.WikiClient;
import com.thinkcube.edutainment.domain.Glossary;
import com.thinkcube.edutainment.domain.GlossaryIndex;
import com.thinkcube.edutainment.domain.OAuth;
import com.thinkcube.edutainment.domain.data.CourseData;
import com.thinkcube.edutainment.domain.dto.CourseDto;
import com.thinkcube.edutainment.domain.dto.OAuthDto;
import com.thinkcube.edutainment.service.SPreferences_;

@EFragment(R.layout.glossary_view)
public class GlossaryFragment extends Fragment implements OnItemClickListener {
	@RestService
	WikiClient wikiClient;

	@Bean
	OAuthDto oAuthDto;

	@RestService
	UserClient userClient;

	@RestService
	GlossaryClient glossaryClient;

	@ViewById(R.id.progressBar)
	ProgressBar progress;
	
	@ViewById(R.id.list)
	ListView glossaryIndex;
	
	@ViewById(R.id.dlist)
	ListView dataList;
	
	@ViewById(R.id.glossary_title)
	TextView courseTitle;

	boolean isFromDashBord;
	
	CourseData courseData;
	
	@ViewById(R.id.notification_msg)
	TextView noData;
	
	ArrayList<GlossaryIndex> indexList=new ArrayList<GlossaryIndex>();
	
	
	
	@AfterViews
	void init(){
		
		setGlossaryIndex();
		
		progress.setVisibility(View.VISIBLE);
		
		courseTitle.setText(courseData.getCourseTitle());
		
		gettoken("A,B");  /* set defalt letter for glossary do not change*/
		
		glossaryIndex.setOnItemClickListener(this);

	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		if(getArguments().containsKey("Course"))
		{
			courseData=getArguments().getParcelable("Course");
			
		}
		
		
	}

	@Trace
	
	@Background
	void gettoken(final String letters) {
		
		
		oAuthDto.setAppId(EdutainmentConstant.APP_ID);
		oAuthDto.setAppSecret(EdutainmentConstant.APP_SCRET);

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					getGlossaryList(userClient.getRequestToken(oAuthDto
							.getMultiValuedMap()),letters);
				} catch (Exception e) {

					showMessge(e.getLocalizedMessage());
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});

	}

	@Bean
	CourseDto courseDto;

	@Pref
	SPreferences_ pref;
	
	@Trace
	@Background
	void getGlossaryList(OAuth requestToken,String letters) {

		// courseDto.setAuthToken("1234");
		courseDto.setAuthToken(requestToken.getoAuth().getToken());
		//courseDto.setCourseId("5275f340d707e2c063249932");
		courseDto.setCourseId(courseData.getCourseId());
		courseDto.setLetters(letters);

		// courseDto.setUserId(pref.getId().get());

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					doGlossaryList(glossaryClient.getGlossary(courseDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});

	}

	@Bean
	GlossaryAdapter glossaryAdapter;

	

	@UiThread
	void doGlossaryList(Glossary glossary) {

		if (glossary != null) {
			
			if (glossary.getGlossaryDataList() != null) {
				
				
				glossaryAdapter.setGlossaryData(glossary.getGlossaryDataList());
								
				dataList.setAdapter(glossaryAdapter);
				
				if(glossary.getGlossaryDataList().size()==0)
				{
					noData.setVisibility(View.VISIBLE);
				}
				
				
				
			}else
			{
				noData.setVisibility(View.VISIBLE);
			}

		}
		
		glossaryAdapter.notifyDataSetChanged();
		
		progress.setVisibility(View.GONE);
		
		
	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}
	
	@Bean
	GlossaryIndexAdapter glossaryIndexAdapter;
	
	void setGlossaryIndex()
	{
		
		
		
		String[] indexArray=getResources().getStringArray(R.array.glossary);
		for (int i = 0; i < indexArray.length; i++) {
			
			GlossaryIndex index= new GlossaryIndex();
			
			index.setIndex(indexArray[i]);
			
			index.setTitle(indexArray[i].replaceAll(",", "-"));
			
			indexList.add(index);
			
		}
		
		glossaryIndexAdapter.setIndexData(indexList);
		
		glossaryIndex.setAdapter(glossaryIndexAdapter);
		
		
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
		
		
		dataList.setAdapter(null);
		
		noData.setVisibility(View.INVISIBLE);
		
		gettoken(indexList.get(position).getIndex());
		
		progress.setVisibility(View.VISIBLE);
		
	}

}
